package com.sc.nft.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.service.UserDaoPerformanceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.UserPerformanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户业绩表服务接口实现
 *
 * @author zxy
 * @since 2023-07-20 17:29:27
 * @description 用户市值
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserPerformanceServiceImpl implements UserPerformanceService {
    private final UserPerformanceDao userPerformanceDao;
    private final UserRelevanceDao userRelevanceDao;
    private final UserDaoPerformanceService userDaoPerformanceService;
    private final OrderDao orderDao;
    private final SecondOrderDao secondOrderDao;
    private final FilterIncomeListDao filterIncomeListDao;
    public static final String DATE_FORMAT="yyyyMMdd";

    @Override
    public BigDecimal getPerformanceNearDate(Long userId, int nearDays,boolean isCenter) {
        DateTime start = DateUtil.offsetDay(DateTime.now(),-nearDays);
        if(isCenter){
            return userPerformanceDao.getCenterSumTermPerformance(userId,DateUtil.format(start,DATE_FORMAT));
        }
        return userPerformanceDao.getSumTermPerformance(userId,DateUtil.format(start,DATE_FORMAT) );
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void calFirstOrder(Order order, String date,Map<Long, Integer> termUser) {
        Long userId=order.getUserId();
        if(userId.longValue()!=1L) {
            UserPerformance userPerformance = getByUserAndDate(userId, date);
            userPerformance.setSelfFirstPerformance(userPerformance.getSelfFirstPerformance().add(order.getProductPrice()));
            userPerformance.updateById();
            List<UserRelevance> upUser = userRelevanceDao.getByRelevance(userId);
            int initLevel = 0; //初始等级 0 普通人 1 工作室 2服务中心
            for (UserRelevance userRelevance : upUser) {
                Integer level = MapUtil.getInt(termUser, userRelevance.getUserId(), 0);
                UserPerformance upUserPerform = getByUserAndDate(userRelevance.getUserId(), date);

                if (initLevel == 0 || level > initLevel) {
                    upUserPerform.setTermFirstPerformance(upUserPerform.getTermFirstPerformance().add(order.getProductPrice()));
                    initLevel = level;
                }
                upUserPerform.setTermFirstCenterSalesVolume(upUserPerform.getTermFirstCenterSalesVolume().add(order.getProductPrice()));
                upUserPerform.updateById();

                if (initLevel == 2) {
                    break;
                }
            }
            userDaoPerformanceService.calFirstOrder(order, date, termUser);
        }
        orderDao.allReadyPerformanceSettleFlag(order.getId());
    }


    private UserPerformance getByUserAndDate(Long userId,String date){
        UserPerformance userPerformance = userPerformanceDao.getByUserIdAndDate(userId, date);
        if(ObjectUtil.isNull(userPerformance)){
            userPerformance = new UserPerformance();
            userPerformance.setUserId(userId);
            userPerformance.setTermFirstPerformance(BigDecimal.ZERO);
            userPerformance.setTermSecondPerformance(BigDecimal.ZERO);
            userPerformance.setTermCenterSalesVolume(BigDecimal.ZERO);
            userPerformance.setTermFirstCenterSalesVolume(BigDecimal.ZERO);
            userPerformance.setTermSecondSalesVolume(BigDecimal.ZERO);
            userPerformance.setSelfFirstPerformance(BigDecimal.ZERO);
            userPerformance.setSelfSecondPerformance(BigDecimal.ZERO);
            userPerformance.setRecordDate(date);
            userPerformance.insert();
        }
        return userPerformance ;
    }

    @Override
    @Transactional
    public void calSecondOrder(SecondOrder order,String date, Map<Long, Integer> termUser) {
        Long userId=order.getBuyUserId();
        if(ObjectUtil.isNull(filterIncomeListDao.getByUserId(userId))) {
            if (userId.longValue() != 1L) {
                UserPerformance userPerformance = getByUserAndDate(userId, date);
                userPerformance.setSelfSecondPerformance(userPerformance.getSelfSecondPerformance().add(order.getCharge()));
                userPerformance.updateById();
                List<UserRelevance> upUser = userRelevanceDao.getByRelevance(userId);
                int initLevel = 0; //初始等级 0 普通人 1 工作室 2服务中心
                for (UserRelevance userRelevance : upUser) {
                    Integer level = MapUtil.getInt(termUser, userRelevance.getUserId(), 0);
                    UserPerformance upUserPerform = getByUserAndDate(userRelevance.getUserId(), date);
                    if (level >= initLevel) {
                        upUserPerform.setTermSecondPerformance(upUserPerform.getTermSecondPerformance().add(order.getCharge()));
                        upUserPerform.setTermSecondSalesVolume(upUserPerform.getTermSecondSalesVolume().add(order.getBuyPrice()));
                        initLevel = level;
                    }
                    upUserPerform.setTermCenterSalesVolume(upUserPerform.getTermCenterSalesVolume().add(order.getBuyPrice()));
                    upUserPerform.updateById();
                    if (initLevel == 2) {
                        break;
                    }
                }
                userDaoPerformanceService.calSecondOrder(order, date, termUser);
            }
        }
        secondOrderDao.allReadyPerformanceSettleFlag(order.getId());
    }

}